var api = require('../config/api.js');
function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatDate(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()


  return [year, month, day].map(formatNumber).join('-')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
function request(url, data = {}, method = "GET") {
  return new Promise(function(resolve, reject) {
      wx.request({
          url: url,
          data: data,
          method: method,
          header: {
              'Content-Type': 'application/json',
              'satoken': wx.getStorageSync('token')

          },
          success: function(res) {
              if (res.statusCode == 200) {
                  resolve(res.data);
              } else {
                  reject(res.errMsg);
              }

          },
          fail: function(err) {
              reject(err)
              console.log("-----返回失败：------------------------------" +
                  "\n-----Code：" + res.statusCode + "======method：" + method + "======errMsg：" + res.errMsg +
                  "\n-----Satoken" + wx.getStorageSync('token') +
                  "\n-----接口：" + url +
                  "\n-----请求参数：" + JSON.stringify(data))
          }
      })
  });
}

function showErrorToast(msg) {
  wx.showToast({
      title: msg,
      image: '/static/images/icon_error.png'
  })
}

function showSuccessToast(msg) {
  wx.showToast({
      title: msg,
  })
}

module.exports = {
  formatTime,
  request,
  showErrorToast,
  showSuccessToast,
  formatDate
}
